package com.tian.view.draw.common

import android.content.Context
import android.graphics.Canvas
import android.graphics.Color
import android.graphics.Paint
import android.graphics.Rect
import android.graphics.RectF
import android.os.Build
import android.util.AttributeSet
import android.view.View
import com.tian.util.view.DisplayUtil

/**
 * @author tiansongtao, Email:tiansongtao@up366.com
 * @date 2022/12/30 9:35
 *
 * canvas 绘制普通图形
 */
class CommonDrawView(context: Context, attrs: AttributeSet?, defStyleAttr: Int) :
        View(context, attrs, defStyleAttr) {

    constructor(context: Context, attrs: AttributeSet?) : this(context, attrs, 0)
    constructor(context: Context) : this(context, null)

    val paint = Paint().apply {
        color = Color.RED
        strokeWidth = DisplayUtil.dpToPx(4f)
        style = Paint.Style.FILL_AND_STROKE
        textSize = DisplayUtil.spToPx(14f)
    }
    val textPaint = Paint().apply {
        color = Color.BLACK
        style = Paint.Style.FILL_AND_STROKE
        textSize = DisplayUtil.spToPx(14f)
    }


    override fun onDraw(canvas: Canvas) {
        super.onDraw(canvas)
        var hStart = 0f
        var lStart = 20f
        /**
         * 画布背景色设置
         * canvas.drawARGB()
         * canvas.drawRGB()
         */
        canvas.drawColor(Color.CYAN)
        /**
         * 写文字，
         * text：要写的文字，
         * x: 写文字的X、Y坐标起始点
         */
        hStart += 60f
        canvas.drawText("drawColor画背景色", lStart, hStart, textPaint)


        //*******************************************************************************
        hStart += 60f
        canvas.drawText("drawLine画线", lStart, hStart, textPaint)
        /**
         * canvas DrawLine 画线, 两点确定一条线
         * startX\startY  起始点x\y坐标
         * stopX\stopY  终止点x\y坐标
         */
        hStart += 30f
        canvas.drawLine(lStart, hStart, 500f, hStart, paint)


        //*******************************************************************************
        hStart += 60f
        canvas.drawText("drawPoint画点", lStart, hStart, textPaint)
        paint.strokeWidth = DisplayUtil.dpToPx(10f)
        /**
         * x、y: 点中心的x\y坐标
         * 注意这里还有paint的宽问题，点的大小就是: paint宽x2
         */
        hStart += 30f
        canvas.drawPoint(30f, hStart, paint)
        paint.strokeWidth = DisplayUtil.dpToPx(4f)


        //*******************************************************************************
        hStart += 60f
        canvas.drawText("drawRect画矩形Rect、RectF", lStart, hStart, textPaint)
        hStart += 30f
        val rect = Rect(20, hStart.toInt(), 500, hStart.toInt() + 70)
        hStart += 70f
        paint.style = Paint.Style.STROKE
        canvas.drawRect(rect, paint)
        paint.style = Paint.Style.FILL_AND_STROKE
        rect.offset(0, 100)
        hStart += 100f
        canvas.drawRect(rect, paint)

        //*******************************************************************************
        hStart += 60f
        canvas.drawText("drawRoundRect画圆角矩形", lStart, hStart, textPaint)
        hStart += 30f
        val rectf = RectF(lStart, hStart, 500f, hStart + 70f)
        hStart += 70f
        /**
         * cx\cy 圆角的大小
         */
        canvas.drawRoundRect(rectf, 20f, 20f, paint)


        //*******************************************************************************
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
            hStart += 60f
            canvas.drawText("drawDoubleRoundRect可以控制的4个角", lStart, hStart, textPaint)
            hStart += 30f
            val outer = RectF(lStart, hStart, 500f, hStart + 100f)
            hStart += 100f
            val outerRadii = floatArrayOf(0f, 0f, 20f, 20f, 0f, 0f, 20f, 20f)
            val inner = RectF(outer)
            inner.inset(20f, 20f)
            paint.style = Paint.Style.STROKE
            //可以控制四个圆角的大小, 只有android10以上才有
            canvas.drawDoubleRoundRect(outer, outerRadii, inner, outerRadii, paint)
            outer.offset(0f, 120f)
            hStart += 120f
            canvas.drawDoubleRoundRect(outer, outerRadii, outer, outerRadii, paint)
        }


        //*******************************************************************************
        hStart += 60f
        canvas.drawText("drawCircle画圆", lStart, hStart, textPaint)
        hStart += 30f
        hStart += 80f
        /**
         * cx\cy 画圆的圆心坐标
         * radius: 画圆的半径
         */
        canvas.drawCircle(100f, hStart, 80f, paint)
        hStart += 80f

        //*******************************************************************************
        hStart += 60f
        canvas.drawText("drawArc画弧线", lStart, hStart, textPaint)
        hStart += 30f
        val arcRectf = RectF(lStart, hStart, 300f, hStart + 200f)
        hStart += 200f
        paint.color = Color.WHITE
        canvas.drawRect(arcRectf, paint)
        paint.color = Color.RED
        paint.style = Paint.Style.STROKE
        /**
         * oval: 画弧线的矩形范围，弧线会充满这个范围
         * startAngle: 画弧线开始的角度，右侧边为0度起始角度，
         * sweepAngle: 要画多大的弧度，弧线的截止点为 startAngle+sweepAngle,
         * 画线为顺时针画
         */
        canvas.drawArc(arcRectf, 0f, 270f, false, paint)

        //*******************************************************************************
        hStart += arcRectf.bottom + 60f
        canvas.drawText("drawOval画椭圆", lStart, hStart, textPaint)
        arcRectf.offset(0f, 270f)
        paint.style = Paint.Style.FILL_AND_STROKE
        paint.color = Color.WHITE
        canvas.drawRect(arcRectf, paint)
        paint.color = Color.RED
        paint.style = Paint.Style.STROKE
        canvas.drawOval(arcRectf, paint)
    }
}